<?php

namespace App\Http\Controllers\Api\AdminCode;

use App\Http\Controllers\Api\AdminCode\Traits\adminRequestUserData;
use App\Http\Controllers\Api\BaseController;
use App\Http\Controllers\Api\Repositories\WxCommentLikeRepositores;
use App\Http\Controllers\Api\Repositories\WxCommentRepositores;
use App\Http\Controllers\Api\Repositories\WxLikeRepositores;
use App\Http\Controllers\Api\Repositories\WxSyncRelationRepositories;
use App\Http\Controllers\Api\Repositories\WxUserRepositores;
use App\Models\Posts\WxComment;
use App\Models\Posts\WxPost;
use App\Models\User\WxUser;
use Illuminate\Http\Request;

class AdminCommentController extends BaseController
{
    use adminRequestUserData;
    /**
     * author,id,posts_id,comment_id,comment_content,comment_img_url
     * 增
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(Request $request){
        // 检查用户是否存在
        if(_empty_($request->author) || _empty_($request->author['id']) || _empty_($request->author['nickName'])){
            return $this->fail(1001001, []);
        }

        // 文章
        $postId = WxSyncRelationRepositories::wx_post_id($request->domain, $request->posts_id);
        if(!$postId){
            return $this->fail(1001003, []);
        }

        // 评论作者
        $uid = WxSyncRelationRepositories::wx_uid($request->domain, $request->author['id']);
        if(_empty_($uid)){
            $data = $this->collectUserData($request);
            $uid = WxUserRepositores::registerUser($data);
            if(!$uid){
                return $this->fail(200002, ['msg'=>'用户数据插入失败']);
            }
        }

        // 父级评论
        $parent_comment_id = null;
        $comment_agent_id = null;
        if($request->comment_id > 0){
            $inner_id = WxSyncRelationRepositories::wx_comment_id($request->domain, $request->comment_id);
            $parent_comment = WxComment::find($inner_id);
            if($parent_comment){
                $parent_comment_id = $parent_comment->id;
                $comment_agent_id = $parent_comment->user_id;
            }else{
                return $this->fail(1001005, []);
            }
        }
        // id
        // $posts_id, $comment_id, $reply_user_id, $user_id, $comment_content, $comment_img_url
        $r = WxCommentRepositores::admin_add($request->domain, $postId, $parent_comment_id, $comment_agent_id, $uid, $request->comment_content, $request->comment_img_url, $request->id, $request->date);
        if($r){
            $c = [];
            if($request->likes){
                $c = $this->likes($request->domain, $request->likes, $request->id, false);
            }
            return $this->success(['msg'=>'ok', 'likes'=>$c]);
        }else{
            return $this->fail(200002, []);
        }
    }

    public function likes($domain, $users, $object,  $is_post = false){
        if(_empty_($users)){
            return [];
        }
        $uids = [];
        foreach ($users as $user){

            $user_data = [];
            $user_data['domain'] = $domain;
            $user_data['mp_openId'] = $user['mp_openId'] ?? '';
            $user_data['web_openId'] = $user['web_openId'] ?? '';
            $user_data['app_openId'] = $user['app_openId'] ?? '';
            $user_data['unionId'] = $user['unionId'] ?: '';
            $user_data['nickName'] = $user['nickName'];
            $user_data['user_background_maps'] = $user['user_background_maps'] ?: '';
            $user_data['avatarUrl'] = $user['avatarUrl'] ?: '';
            $user_data['gender'] = $user['gender'] ?: 0;
            $user_data['external_id'] = $user['id'];
            $user_data['date'] = $user['date'];
            $uid = WxUserRepositores::registerUser($user_data);
            if(!$uid){
                continue;
            }
            $uids[] = $uid;
        }

        $rs = [];
        if($is_post){

            foreach ($uids as $uid){
                $postId = WxSyncRelationRepositories::wx_post_id($domain, $object);
                if($postId){
                    $r = WxLikeRepositores::admin_like($uid, $postId);
                    $rs[] = $r;
                }
            }

        }else{
            foreach ($uids as $uid){
                $commentId = WxSyncRelationRepositories::wx_comment_id($domain, $object);
                if($commentId) {
                    $r = WxCommentLikeRepositores::like($uid, $commentId);
                    $rs[] = $r;
                }
            }

        }

        return $rs;
    }

    /**
     * 删
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request){
        $inner_id = WxSyncRelationRepositories::wx_comment_id($request->domain, $request->id);
        if(!$inner_id){
            return $this->fail(200003, ['msg'=>'没有对应的评论']);
        }
        if( WxComment::where('id', $inner_id)->delete() ){
            return $this->success(['msg'=>'ok']);
        }else{
            return $this->fail(200002, ['msg'=>'评论删除失败']);
        }
    }

    /**
     * 改
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(Request $request){

    }

}
